<template>
  <el-dialog class="dia-style" :title="title" :visible.sync="isOpen" width="60%" :close-on-click-modal="false">
    <el-form ref="queryForm" :model="queryParams" size="small" :inline="true" @submit.native.prevent>
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入任务名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          :header-cell-style="{ background: '#F3F5FC' }"
          @click="handleQuery"
        >查询
        </el-button>
      </el-form-item>
    </el-form>
    <HkTable
      ref="table"
      :total="total"
      :loading="loading"
      :table-data="tableData"
      :table-columns="tableLabelList"
      :query-params="queryParams"
      :show-pag="true"
      :selection="true"
      @changePage="getList"
      @handleSelectionChange="selectionChange"
    >
      <!-- <template #taskName="scope">
        <el-tooltip placement="bottom" effect="light">
          <div slot="content">
            {{ scope.row.taskName }}
          </div>
          <span class="tablelist-title">{{ scope.row.taskName }}</span>
        </el-tooltip>
      </template> -->
    </HkTable>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">保 存</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import HkTable from '@/components/Hk-Table'
import { addScheduTaskList } from '@/api/milkyway/jobScheduling'

export default {
  name: 'SelectDialog',
  components: { HkTable },
  props: {
    title: {
      type: String,
      default() {
        return '编辑'
      }
    },
    currentData: {
      type: Array,
      default() {
        return []
      }
    },
    type: {
      type: String,
      default() {
        return ''
      }
    },
    currentId: {
      type: String,
      default() {
        return ''
      }
    }
  },
  data() {
    return {
      loading: false,
      total: 0,
      tableData: [],
      tableLabelList: [
        { label: '任务名称', param: 'taskName' },
        { label: '接入数据源', param: 'datasourceName', width: '240' },
        { label: '输出数据源', param: 'outputDatasourceName' }
      ],
      checkNodes: [],
      queryParams: {
        id: '',
        name: '',
        type: this.type,
        current: 0,
        size: 50
      },
      isOpen: false
    }
  },
  watch: {
    type: {
      handler(newVal) {
        this.queryParams.type = newVal
      }
    },
    currentData(newVal) {
      if (newVal.length >= 0) {
        this.checkNodes = newVal
      }
    },
    currentId: {
      handler(newVal) {
        this.queryParams.id = newVal
      }
    }
  },
  mounted() {
    // this.getList()
  },
  methods: {
    /** 获取来的值重新获取*/
    initSelect(row) {
      this.$nextTick(() => {
        if (row) {
          this.tableData.forEach((item) => {
            row.forEach((i) => {
              if (i.id == item.id) {
                this.$refs.table.toggleRow(item)
              }
            })
          })
        }
      })
    },
    /** 显示弹窗*/
    show() {
      this.isOpen = true
      this.getList()
    },
    /** 查询角色列表 */
    getList() {
      this.loading = true
      addScheduTaskList(this.queryParams).then((res) => {
        this.loading = false
        this.tableData = res.list
        this.total = res.total
        this.initSelect(this.checkNodes)
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$emit('handleSubmit', this.checkNodes)
      this.isOpen = false
    },
    // 取消按钮
    cancel() {
      this.isOpen = false
      this.reset()
    },
    /**  多选框 */
    selectionChange(data) {
      this.checkNodes = data
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const name = row.name
      this.$modal
        .confirm('是否确认删除角色编号为"' + name + '"的数据项？')
        .then(function() {
          return true
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 重置*/
    reset() {
      this.$nextTick(() => {
        this.resetForm('queryForm')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-pagination{
  position:relative !important;
}
::v-deep .targetTable{
  overflow: hidden;
}
::v-deep .el-table {
  margin-top: 0px !important;
  height: calc(100% - 85px);
  min-height:460 !important;
}
.tablelist-title {
  display: block;
  overflow: hidden;
  white-space: nowrap;
  -o-text-overflow: ellipsis;
  text-overflow: ellipsis;
}
.el-cascader {
  width: 100%;
}

.el-dialog__wrapper {
  margin-bottom: 0px;
}
</style>
